package com.frameworkc.ffmpeg;

import android.view.Surface;

public class FFmpegPlayer {
    static {
        System.loadLibrary("ffmpeg");
    }
    private long nativeHandle;
    private OnPrepareListener onPrepareListener;
    private OnErrorListener onErrorListener;
    private OnProgressListener onProgressListener;

    public FFmpegPlayer(){
        nativeHandle = FFmpegInit();
    }
    public void prepare(){
        prepare(nativeHandle);
    }
    public void setDataSource(String path){
        setDataSource(nativeHandle,path);
    }
    public void setSurface(Surface surface) {
        setSurface(nativeHandle, surface);
    }
    public void start() {
        start(nativeHandle);
    }
    public void setOnPrepareListener(OnPrepareListener onPrepareListener) {
        this.onPrepareListener = onPrepareListener;
    }
    public void setOnErrorListener(OnErrorListener onErrorListener) {
        this.onErrorListener = onErrorListener;
    }
    public void setOnProgressListener(OnProgressListener onProgressListener) {
        this.onProgressListener = onProgressListener;
    }

    public void onParpare(){
        if (null != onPrepareListener) {
            onPrepareListener.onPrepared();
        }
    }
    public void onError(int errorCode) {
        if (null != onErrorListener) {
            onErrorListener.onError(errorCode);
        }
    }
    public void onProgress(int progress){
        if (null != onProgressListener) {
            onProgressListener.onProgress(progress);
        }
    }
    private native long FFmpegInit();
    private native void prepare(long nativeHandle);
    private native void start(long nativeHandle);
    private native void stop(long nativeHandle);
    private native void setDataSource(long nativeHandle, String path);
    private native void setSurface(long nativeHandle, Surface surface);

    public void stop() {
        stop(nativeHandle);
    }

    public interface OnPrepareListener {
        void onPrepared();
    }
    public interface OnErrorListener {
        void onError(int err);
    }
    public interface OnProgressListener {
        void onProgress(int progress);
    }
}
